<?php
/**
 * @author Truong Kim Phung
 * @copyright 2014
 * @description: save data category
 */
// not login
// not perm add
if( !isset($AppUI) ||
    !isset($_SESSION['category']) ||
    (isset($_SESSION['category']) && ($_SESSION['category']&1<<2)==0)

) {
	redirect(HOST_BACKEND);
}
// data input
$intCategoryId		= $this->input->post('pcategoryid', 0);
$intType			= $this->input->post('ptype', 0);
$strName			= $this->input->post('pname', '');
$strSlug			= $this->input->post('pslug', '');
$strTag				= $this->input->post('ptag', '');
$strShort			= $this->input->post('pshort', '');
$oIcon				= $_FILES['pimage'];
$oIconAct			= $_FILES['pimage2'];
$intIseq			= $this->input->post('piseq', 0);
$arrErr             = array();
$strIcon            = '';
$strIconAct         = '';
// strip tag data input
$strName            = CI_Function::stripTag($strName,'name');
$lenName            = strlen($strName);
$strShort           = CI_Function::stripTag($strShort,'short');
$lenShort           = strlen($strShort);
$strTag             = CI_Function::stripTag($strTag,'tag');
// validate manual	
//+ name	
if(!isset($strName) || empty($strName))
    array_push($arrErr, $arrLocale['name_required']);
elseif(isset($strName) && $lenName < VL_NAME_MINLENGTH ) 
    array_push($arrErr, $arrLocale['name_minlength']);
//+ short
if($strShort != '' && $lenShort > VL_SHORT_MAXLENGTH) 
    array_push($arrErr, $arrLocale['short_maxlength']);


if(isset($arrErr) && count($arrErr)==0) {//allow save
    // unset session if exists
    if(isset($_SESSION['CATEGORY_ERROR'])) unset($_SESSION['CATEGORY_ERROR']);
    if(isset($_SESSION['CATEGORY_POST'])) unset($_SESSION['CATEGORY_POST']);
    $this->load->model('category_model', 'category');	    
    $intError = $this->category->update($AppUI->id, $intCategoryId, $strName, $strSlug, $strShort, 
										$strIcon, $strIconAct, $intIseq, $intType, $strTag);
  
    if($intError==0) CI_Function::setMsg($arrMessage['save_update']);
    elseif($intError==1) array_push($arrErr, $arrError['noperm_update']);        
}
// occur error

if (isset($arrErr) && count($arrErr) > 0) {
    $_SESSION['CATEGORY_ERROR']    = $arrErr;    
    $_SESSION['CATEGORY_POST']     = $this->input->post();
    
    redirect(HOST_BACKEND . 'category/edit?type=' . $intType . '&categoryid=' . $intCategoryId);
}
redirect(HOST_BACKEND.'category');

